package dgt;
import java.io.BufferedOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class PublicServlet extends DGTAbstractServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
		resp.setContentType("application/vnd.google-earth.kmz");
		resp.setHeader("Content-Disposition", "attachment; filename=dgt.kmz");
		
		resp.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
		// Set standard HTTP/1.1 no-cache headers.
		resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
		resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
		// Set standard HTTP/1.0 no-cache header.
		resp.setHeader("Pragma", "no-cache");
		
		try{
			resp.flushBuffer();
			byte[] kmz = getKmzFromCache();
			if(kmz == null){
				logger.info("OMG! KMZ not found in cache!");
				kmz = grabKmz();
				cacheKmz(kmz);
			}
			BufferedOutputStream output = new BufferedOutputStream(resp.getOutputStream());
			output.write(kmz);
			output.flush();
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}
}
